<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Groupmodel extends CI_Model {

	protected $_CI;

	public static $searchField = array();
	public static $likeField = array();
	public static $sortField = array();

	public function __construct(){
		parent::__construct();
		$this->_CI = & get_instance();
	}
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
	public function getGroupListSection($assist=array()){
		$this->db->from('group');
		$count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

		$this->db->order_by('group_id','asc');
		if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
		$query = $this->db->get();
		$list = $query->result_array();

		return array($list,$count);
	}

	public function getGroupPermission($group_id){
		$this->db->select('group_permission');
		$this->db->from('group');
		$this->db->where('group_id', $group_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$group_permission = $query->row_array();

		$permission = array();
		if(!empty($group_permission) && validate($group_permission['group_permission'])){
			$permission = json_decode($group_permission['group_permission']);

			if(!is_array($permission) || empty($permission)){
				$permission = array();
			}
		}

		return $permission;
	}

	public function getGroup($group_id){
		$this->db->from('group');
		$this->db->where('group_id',$group_id);
		$this->db->limit(1);
		$query = $this->db->get();
		$group = $query->row_array();

		return $group;
	}

	public function getGroupAction($group_id){
		$this->db->select('group_action');
		$this->db->from('group');
		$this->db->where('group_id', $group_id);
		$query = $this->db->get();
		$action = $query->result_array();
		
		return $action;
	}

	public function getGroupActionTree($actionList = array()) {
		global $g_menu;
		$menu = $g_menu;
		$menu = $this->_array_remove_empty($menu);
		$row1 = array();
		$flg1 = true;
		$flg1_count = 0;
		foreach ($menu as $k0 => $v0) {
			$row2 = array();
			$flg2 = true;
			$flg2_count = 0;
			foreach ($v0 as $k1 => $v1) {
				$row3 = array();
				$flg3 = true;
				$flg3_count = 0;
				foreach ($v1 as $k2 => $ctrller) {
					if(!class_exists($ctrller)&&file_exists(APPPATH.'controllers/admin/'.$ctrller.EXT)){
						require(APPPATH.'controllers/admin/'.$ctrller.EXT);
					}
					if(file_exists(APPPATH.'controllers/admin/'.$ctrller.EXT)){
						$class_methods = get_class_methods($ctrller);	
					}
 					
 					$row4 = array();
 					$flg4 = true;
 					$flg4_count = 0;
					foreach($class_methods as $method){
						if(!in_array($method,array('__construct','__destruct','get_instance'))){
							$action = $ctrller.'/'.$method;
							$status = in_array($action,$actionList);
							$row4[] = array(
								'text' => $action,
								'children' => array(),
								'checked' => $status,
								'leaf' => true,
								'value' => $action,
							);
							if(!$status) {
								$flg4 = false;
							} else {
								$flg4_count ++;
							}
						}
					}
					$row3[] = array(
						'text' => $k2."({$flg4_count})",
						'expanded' => false,
						'checked' => $flg4,
						'children' => $row4,
					);
					if(!$flg4) {
						$flg3 = false;
					} else {
						$flg3_count += $flg4_count;
					}
				}
				$row2[] = array(
					'text' => $k1."({$flg3_count})",
					'expanded' => true,
					'checked' => $flg3,
					'children' => $row3,
				);
				if(!$flg3) {
					$flg2 = false;
				} else {
					$flg2_count += $flg3_count;
				}
			}
			$row1[] = array(
				'text' => $k0."({$flg2_count})",
				'expanded' => true,
				'children' => $row2,
				'checked' => $flg2,
			);
			if(!$flg2) {
				$flg1 = false;
			} else {
				$flg1_count += $flg2_count;
			}
		}
		$tree = array(
			array(
				'text' => $this->lang->line('tree_root')."({$flg1_count})",
				'expanded' => true,
				'children' => $row1,
				'checked' => $flg1,
			)
		);

		return $tree;
	}

	private function _array_remove_empty($haystack) {
		foreach ($haystack as $key => $value) {
			if (!empty($value) && is_array($value)) {
				$haystack[$key] = $this->_array_remove_empty($haystack[$key]);
			}
			if (empty($value)) {
				unset($haystack[$key]);
			}
		}

		return $haystack;
	}
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
	public function createGroup($info){
		$this->db->insert('group',$info);
		return $this->db->insert_id();
	}
/*
| -------------------------------------------------------------------
|  EDIT Functions
| -------------------------------------------------------------------
*/
	public function editGroup($info){
		$this->db->where('group_id',$info['group_id']);
		unset($group_id);
		$this->db->update('group',$info);
	}
/*
| -------------------------------------------------------------------
|  Extern Functions
| -------------------------------------------------------------------
*/
	public function outputGroupList4js(){
		$this->db->select('group_id,group_name');
		$this->db->from('group');
		$query = $this->db->get();
		$list = $query->result_array();

		$result = array();
		foreach ($list as $record) {
			$result[] = array($record['group_id'],$record['group_name']);
		}

		return json_encode($result);
	}

	public function outputAvailableGroupList4js() {
		$user = $this->session->userdata('user');
		$result = array();
		if(!empty($user)) {
			$this->db->select('group_id,group_name');
			$this->db->from('group');
			$this->db->where('group_level <=',$user['group_level']);
			$query = $this->db->get();
			$list = $query->result_array();

			foreach ($list as $record) {
				$result[] = array($record['group_id'],$record['group_name']);
			}
		}
		return json_encode($result);
	}

	public function checkGroupLevelAvailable($group_id) {
		$user = $this->session->userdata('user');
		$adduser_group = $this->getGroup($group_id);
		return intval($user['group_level']) >= intval($adduser_group['group_level']);
	}
}